<?php

namespace App\Http\Controllers\Config;

use App\Models\Config\WeChat;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class WeChatController extends Controller
{
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    }
    
    public function index(Request $request)
    {
        if ($request->ajax()) {

            $wechat= WeChat::get();

            $rows= [];
            foreach ($wechat as $val){
                $rows[] = $val;
            }

            $data = [
                'rows' => $rows,
            ];

            return response()->json($data);

        } else {
            return view('/config/wechat/index');
        }
    }

    public function edit(Request $request)
    {
        $oper = $request->get('oper');

        if ($oper == "add"){

            $wechat = new WeChat();

            $wechat->WECHAT_KEY = $request->get('WECHAT_KEY');
            $wechat->NOTIFY_URL = $request->get("NOTIFY_URL");
            $wechat->APPID = $request->get('APPID');
            $wechat->MCH_ID = $request->get('MCH_ID');

            $wechat->save();
        }
        if ($oper == "edit"){

            $id = $request->get("id");

            $wechat = WeChat::find($id);
            $wechat->WECHAT_KEY = $request->get('WECHAT_KEY');
            $wechat->NOTIFY_URL = $request->get("NOTIFY_URL");
            $wechat->APPID = $request->get('APPID');
            $wechat->MCH_ID = $request->get('MCH_ID');

            $wechat->save();

        }elseif($oper == "del"){
            $id = $request->get("id");

            $wechat = WeChat::find($id);
            $wechat->delete();
        }

        return response()->json(["success" => true]);
    }

}
